import delimited "../2_Data\raw\2020_IT_Region_Mobility_Report.csv", clear 

keep if sub_region_1!="" & sub_region_2==""

replace iso_3166_2_code = subinstr(iso_3166_2_code,"IT-","",.)

rename sub_region_1 region

replace region=strupper(region)

gen year = substr(date,1,4)
gen month = substr(date, 6,2)
gen day = substr(date, 9,2)

replace date = day+"/"+month+"/"+year

gen ddate = date(date,"DMY")
drop date
rename ddate date

order date region 
sort region date

rename (retail_and_recreation_percent_ch grocery_and_pharmacy_percent_cha parks_percent_change_from_baseli ///
transit_stations_percent_change_ workplaces_percent_change_from_b residential_percent_change_from_) (retail grocery parks stations workplaces residential)

replace region="VALLE D'AOSTA" if region=="AOSTA"
replace region="PUGLIA" if region=="APULIA"
replace region="FRIULI VENEZIA GIULIA" if region=="FRIULI-VENEZIA GIULIA"
replace region="LOMBARDIA" if region=="LOMBARDY"
replace region="PIEMONTE" if region=="PIEDMONT"
replace region="SARDEGNA" if region=="SARDINIA"
replace region="SICILIA" if region=="SICILY"
replace region="TRENTINO-ALTO ADIGE" if region=="TRENTINO-SOUTH TYROL"
replace region="TOSCANA" if region=="TUSCANY"

merge 1:1 region date using "../2_Data/temp/data_selected_region.dta", assert(match master) keep(match) nogen


cap gen aux=.

preserve

local var workplaces

cap gen aux=.

gen  mean_`var'=0
gen ciu_`var' = 0
gen cil_`var' = 0
forvalues week = 27 / 42 {
cap reg `var' if election==1 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==1 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==1 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==1 & week==`week'

cap reg `var' if election==0 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==0 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==0 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==0 & week==`week'

}


local labfigure: variable label `var'

local figure "_`var'_raw"

twoway (line aux week if election==1, color($color_election) lwidth(vvthick)) (line aux week if election==0, color($color_noelection) lwidth(vvthick)) (rcap ciu_`var' cil_`var' week if election==1, lpattern("--") color($color_election)) (rcap ciu_`var' cil_`var' week if election==0, lpattern("--") color($color_noelection)) ///  
(line mean_`var' week if election==1, sort lcolor($color_election) lwidth(medthick)) ///
(line mean_`var' week if election==0, sort lcolor($color_noelection) lwidth(medthick)), ///
xtitle("Week", size(large)) xline(38, lcolor(gs10) lpattern("--"))  ///
xline(34, lcolor(gs10) lpattern("--")) graphregion(fcolor(white) color(white)) scheme(s1mono) legend(size() bmargin(medsmall) order(2 1) label(1 "Campaign") label(2 "No Campaign") cols(1) region(lcolor(white)) just(left) position(11) ring(0) bplacement(nw)) /// 
xlabel(27 "Jul. 5" 28 "Jul. 12" 29 "Jul. 19" 30 "Jul. 26" 31 "Aug. 2" 32 "Aug. 9" 33 "Aug. 16" 34 "Aug. 23" 35 "Aug. 30" 36 "Sep. 6" 37 "Sep. 13" 38 "Sep. 20" 39 "Sep. 27" 40 "Oct. 4" 41 "Oct. 11" 42 "Oct. 18", labsize() angle(45)) ///
ylabel(-50(25)0, angle(0) labsize(large))


restore



preserve

local var retail

cap gen aux=.

gen  mean_`var'=0
gen ciu_`var' = 0
gen cil_`var' = 0
forvalues week = 27 / 42 {
cap reg `var' if election==1 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==1 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==1 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==1 & week==`week'

cap reg `var' if election==0 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==0 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==0 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==0 & week==`week'

}


local labfigure: variable label `var'

local figure "_`var'_raw"

twoway (line aux week if election==1, color($color_election) lwidth(vvthick)) (line aux week if election==0, color($color_noelection) lwidth(vvthick)) (rcap ciu_`var' cil_`var' week if election==1, lpattern("--") color($color_election)) (rcap ciu_`var' cil_`var' week if election==0, lpattern("--") color($color_noelection)) ///  
(line mean_`var' week if election==1, sort lcolor($color_election) lwidth(medthick)) ///
(line mean_`var' week if election==0, sort lcolor($color_noelection) lwidth(medthick)), ///
xtitle("Week", size(large)) xline(38, lcolor(gs10) lpattern("--"))  ///
xline(34, lcolor(gs10) lpattern("--")) graphregion(fcolor(white) color(white)) scheme(s1mono) legend(size() bmargin(medsmall) order(2 1) label(1 "Campaign") label(2 "No Campaign") cols(1) region(lcolor(white)) just(left) position(11) ring(0) bplacement(nw)) /// 
xlabel(27 "Jul. 5" 28 "Jul. 12" 29 "Jul. 19" 30 "Jul. 26" 31 "Aug. 2" 32 "Aug. 9" 33 "Aug. 16" 34 "Aug. 23" 35 "Aug. 30" 36 "Sep. 6" 37 "Sep. 13" 38 "Sep. 20" 39 "Sep. 27" 40 "Oct. 4" 41 "Oct. 11" 42 "Oct. 18", labsize() angle(45)) ///
ylabel(-25(25)25, angle(0) labsize(large))

restore

preserve

local var stations

cap gen aux=.

gen  mean_`var'=0
gen ciu_`var' = 0
gen cil_`var' = 0
forvalues week = 27 / 42 {
cap reg `var' if election==1 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==1 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==1 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==1 & week==`week'

cap reg `var' if election==0 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==0 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==0 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==0 & week==`week'

}


local labfigure: variable label `var'

local figure "_`var'_raw"

twoway (line aux week if election==1, color($color_election) lwidth(vvthick)) (line aux week if election==0, color($color_noelection) lwidth(vvthick)) (rcap ciu_`var' cil_`var' week if election==1, lpattern("--") color($color_election)) (rcap ciu_`var' cil_`var' week if election==0, lpattern("--") color($color_noelection)) ///  
(line mean_`var' week if election==1, sort lcolor($color_election) lwidth(medthick)) ///
(line mean_`var' week if election==0, sort lcolor($color_noelection) lwidth(medthick)), ///
xtitle("Week", size(large)) xline(38, lcolor(gs10) lpattern("--"))  ///
xline(34, lcolor(gs10) lpattern("--")) graphregion(fcolor(white) color(white)) scheme(s1mono) legend(size() bmargin(medsmall) order(2 1) label(1 "Campaign") label(2 "No Campaign") cols(1) region(lcolor(white)) just(left) position(11) ring(0) bplacement(nw)) /// 
xlabel(27 "Jul. 5" 28 "Jul. 12" 29 "Jul. 19" 30 "Jul. 26" 31 "Aug. 2" 32 "Aug. 9" 33 "Aug. 16" 34 "Aug. 23" 35 "Aug. 30" 36 "Sep. 6" 37 "Sep. 13" 38 "Sep. 20" 39 "Sep. 27" 40 "Oct. 4" 41 "Oct. 11" 42 "Oct. 18", labsize() angle(45)) ///
ylabel(-30(0)30, angle(0) labsize(large))

restore


preserve

local var residential

cap gen aux=.

gen  mean_`var'=0
gen ciu_`var' = 0
gen cil_`var' = 0
forvalues week = 27 / 42 {
cap reg `var' if election==1 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==1 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==1 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==1 & week==`week'

cap reg `var' if election==0 & week==`week', r
cap replace mean_`var' = _b[_cons] if election==0 & week==`week'
cap drop se
cap gen se = _se[_cons]

cap replace ciu_`var' = mean_`var'+1.96*se if election==0 & week==`week'
cap replace cil_`var' = mean_`var'-1.96*se if election==0 & week==`week'

}


local labfigure: variable label `var'

local figure "_`var'_raw"

twoway (line aux week if election==1, color($color_election) lwidth(vvthick)) (line aux week if election==0, color($color_noelection) lwidth(vvthick)) (rcap ciu_`var' cil_`var' week if election==1, lpattern("--") color($color_election)) (rcap ciu_`var' cil_`var' week if election==0, lpattern("--") color($color_noelection)) ///  
(line mean_`var' week if election==1, sort lcolor($color_election) lwidth(medthick)) ///
(line mean_`var' week if election==0, sort lcolor($color_noelection) lwidth(medthick)), ///
xtitle("Week", size(large)) xline(38, lcolor(gs10) lpattern("--"))  ///
xline(34, lcolor(gs10) lpattern("--")) graphregion(fcolor(white) color(white)) scheme(s1mono) legend(size() bmargin(medsmall) order(2 1) label(1 "Campaign") label(2 "No Campaign") cols(1) region(lcolor(white)) just(left) position(11) ring(0) bplacement(nw)) /// 
xlabel(27 "Jul. 5" 28 "Jul. 12" 29 "Jul. 19" 30 "Jul. 26" 31 "Aug. 2" 32 "Aug. 9" 33 "Aug. 16" 34 "Aug. 23" 35 "Aug. 30" 36 "Sep. 6" 37 "Sep. 13" 38 "Sep. 20" 39 "Sep. 27" 40 "Oct. 4" 41 "Oct. 11" 42 "Oct. 18", labsize() angle(45)) ///
ylabel(-6 "-6.0" "0" 6 "6.0", angle(0) labsize(large))


restore
